package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{

	@Autowired
	private CustomerRepository customerRepository;
	
	//查询没有关联的用户
	@Override
	public List<Customer> findNoAssociationsCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
	}

	//查询关联的客户
	@Override
	public List<Customer> findHasAssociationsCustomer(String fixedAreaId) {
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	//将客户与定区关联
	@Override
	public void associationsCustomer(String customerIds, String fixedAreaId) {
		
		//关联之前将已经关联的客户全部取消关联
		customerRepository.clearAssociations(fixedAreaId);
		
		if ("null".equals(customerIds)) {
			return ;
		}
		//将select框中的客户关联
		String[] ids = customerIds.split(",");
		for (String customerId : ids) {
			Integer id = Integer.parseInt(customerId);
			customerRepository.updateFixedAreaId(fixedAreaId, id);
		}
	}

	@Override
	public void saveCustomer(Customer customer) {
		customerRepository.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
		
	}

	@Override
	public Customer login(String telephone, String password) {
		return customerRepository.findByTelephoneAndPassword(telephone,password);
	}



	
}
